
import time
from selenium.webdriver.common.by import By
from selenium.webdriver.support.select import Select

from common.Tools import Tools
from page.admin.AdminLoginPage import AdminLoginPage
from base.BasePage import BasePage
from page.admin.AdminMallPage import AdminMallPage


class AdminGoodsListPage(BasePage):
    def __init__(self,driver):
         super().__init__(driver)

    # 添加商品
    def add_goods(self,good_name,cat1_text,cat2_text,cat3_text,shop_price,market_price,is_free_shipping=None):
        # 1.iframe切换
        self.driver.switch_to.frame(self.find_ele((By.ID, "workspace")))
        # 2.进入添加商品界面
        self.find_ele((By.LINK_TEXT, "添加商品")).click()
        # 3.商品名称
        self.input_text(self.find_ele((By.NAME, "goods_name")), good_name)
        # 4.商品分类
        select1 = Select(self.find_ele((By.ID, "cat_id")))
        select1.select_by_visible_text(cat1_text)
        time.sleep(1)
        select1 = Select(self.find_ele((By.ID, "cat_id_2")))
        select1.select_by_visible_text(cat2_text)
        time.sleep(1)
        select1 = Select(self.find_ele((By.ID, "cat_id_3")))
        select1.select_by_visible_text(cat3_text)
        # 5.本店售价
        self.input_text(self.find_ele((By.NAME, "shop_price")), shop_price)
        # 6、市场价
        self.input_text(self.find_ele((By.NAME, "market_price")), market_price)
        # 7.是否包邮
        if is_free_shipping is not None:
            self.find_ele((By.ID, "is_free_shipping_label_0")).click()
            time.sleep(0.5)
            # 选择运费模板
            select1 = Select(self.find_ele((By.NAME, "template_id")))
            select1.select_by_visible_text(is_free_shipping)
        # 8.提交
        self.find_ele((By.ID, "submit")).click()

    # 搜索商品
    def search_goods(self,good_name):
        # 1.输入商品名称
        self.input_text(self.find_ele((By.NAME, "key_word")), good_name)
        # 2.点击搜索
        self.find_ele((By.CSS_SELECTOR, "input[value='搜索']")).click()




if __name__ == '__main__':
    driver = Tools.web_driver()
    driver.get("http://hmshop-test.itheima.net/admin")
    admin_login = AdminLoginPage()
    admin_login.login("admin", "HM_2025_test", 8888)
    admin_mall = AdminMallPage()
    admin_mall.go_to_goods_list()
    admin_goods_list = AdminGoodsListPage()
    admin_goods_list.add_goods("web测试", "服饰", "女装", "新品推荐",99,99,"以件计算")
    time.sleep(2)
    driver.quit()